using Unity.Entities;
using Unity.Entities.Serialization;
using UnityEngine;

namespace DOTS.DOD.LESSON10
{
    struct RespawnController : IComponentData
    {
        public float Timer;
    }

    struct PrefabBufferElement : IBufferElementData
    {
        public EntityPrefabReference Prefab;
    }

    public class RespawnControllerAuthoring : MonoBehaviour
    {
        public GameObject[] Spawners;
        public float Timer;

        class Baker : Baker<RespawnControllerAuthoring>
        {
            public override void Bake(RespawnControllerAuthoring authoring)
            {
                var entity = GetEntity(TransformUsageFlags.None);
                AddComponent(entity, new RespawnController()
                {
                    Timer = authoring.Timer,
                });

                var buffer = AddBuffer<PrefabBufferElement>(entity);
                foreach (var spawner in authoring.Spawners)
                {
                    buffer.Add(new PrefabBufferElement()
                    {
                        Prefab = new EntityPrefabReference(spawner),
                    });
                }
            }
        }
    }
}